package com.ulife.permission.module.screen;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.Navigator;
import com.ulife.common.ErrorMessage;
import com.ulife.common.Result;
import com.ulife.common.exception.ManagerException;
import com.ulife.permission.common.FunctionEnum;
import com.ulife.permission.common.RoleQueryResult;
import com.ulife.permission.common.TargetEnum;
import com.ulife.permission.dao.dataobject.RoleDO;
import com.ulife.permission.manager.RoleManager;

public class RoleInfo {

	private static final Logger log = LoggerFactory.getLogger(RoleInfo.class);

	@Autowired
	private RoleManager roleManager;
	
	@Autowired
	private HttpServletRequest request;
	
	public void execute(Context context, Navigator nav) {
		try {
			context.put("module", "role");
			String roleIdStr = request.getParameter("role_id");
			if (StringUtils.isNotBlank(roleIdStr) && StringUtils.isNumeric(roleIdStr)) {
				long roleId = Long.parseLong(roleIdStr);
				Result<RoleDO> roleResult = roleManager.getRoleById(roleId);
				if (roleResult != null && roleResult.isSuccess() && roleResult.getModule() != null) {
					RoleDO role = roleResult.getModule();
					if (role.getStatus() != 0) {
						context.put("error", "该角色不可用");
						nav.forwardTo("error");
					}
					String targetList = role.getTargetList();
					String functionList = role.getFunctionList();
					Map<String, Long> targetMap = buildMap(targetList);
					Map<String, Long> functionMap = buildMap(functionList);
					TargetEnum.values();
					context.put("targetMap", targetMap);
					context.put("functionMap", functionMap);
					context.put("role", role);
					context.put("targets", TargetEnum.values());
					context.put("functions", FunctionEnum.values());
					
					RoleQueryResult result = roleManager.getAllRole();
					List<RoleDO> roleList = null;
					if (CollectionUtils.isNotEmpty(result.getRoleList())){
						roleList = result.getRoleList();
					}
					context.put("roles", roleList);
				}
				else {
					context.put("error", ErrorMessage.ROLE_INFO_ERROR);
					nav.forwardTo("error");
				}
			}
			else {
				context.put("error", ErrorMessage.ROLE_INFO_ERROR);
				nav.forwardTo("error");
			}
		} catch (ManagerException e) {
			context.put("error", ErrorMessage.ROLE_INFO_ERROR);
			nav.forwardTo("error");
			log.error("get user list error",e);
		}
	}

	private Map<String, Long> buildMap(String list) {
		Map<String, Long> map = new HashMap<String, Long>();
		if (StringUtils.isNotBlank(list)) {
			String[] strings = StringUtils.split(list, ",");
			for (String s : strings) {
				map.put(s, 1L);
			}
		}
		return map;
	}

}
